<%@ page language="java" pageEncoding="UTF-8"%>
<%@ include file="/system/include/taglibs.jsp"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">    
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link rel="stylesheet" type="text/css" href="${ctx}/ext3.2/resources/css/ext-all.css" />
<link rel="stylesheet" type="text/css" href="${ctx}/ext3.2/ux/css/RowEditor.css" />
    
<script type="text/javascript" src="${ctx}/ext3.2/adapter/ext/ext-base.js"></script>
<script type="text/javascript" src="${ctx}/ext3.2/ext-all.js"></script>
<script type="text/javascript" src="${ctx}/ext3.2/ux/RowEditor.js"></script>
<script type="text/javascript">
Ext.onReady(function() {
	 Ext.QuickTips.init();
	 var sm = new Ext.grid.CheckboxSelectionModel();
	 var cm = new Ext.grid.ColumnModel([
	    sm,
		{header: "编号", width: 100, dataIndex: 'id'}, 
		{header: "字典名称", width: 100, sortable: true, dataIndex: 'dictName', 
				editor: {xtype: 'textfield'}},
		{header: "字典码", width: 100, dataIndex: 'dictCode',editor: {xtype: 'textfield'}},
		{header: "类编码", width: 100, dataIndex: 'classCode',editor: {xtype: 'textfield'}},
		{header: "类值", width: 100, dataIndex: 'classValue',editor: {xtype: 'textfield'}},
		{header: "子字典码", width: 100, dataIndex: 'subdictCode',editor: {xtype: 'textfield'}}                     		
	 ]);
	 
	 var Dict = Ext.data.Record.create([
		{name: 'id',type: 'int'}, 
		{name: 'dictName',type: 'string'}, 
		{name: 'dictCode',type: 'string'},
		{name: 'classCode',type: 'string'},
		{name: 'classValue',type: 'string'},
		{name: 'subdictCode',type: 'string'}
	]);
		
		var reader = new Ext.data.JsonReader({ 
			root: 'rows',  //rows : 为服务器端传送过来的记录数值对象
			totalProperty: 'limit' //results 为服务器端json传送过来的记录的行数, count(*)运算
			//id: 'id'
		}, [
			'id', 
			'dictName',
			'dictCode',
			'classCode',
			'classValue',
			'subdictCode'
		]);

		var findall = "appDict_all.do";
		
		var store = new Ext.data.Store({ 
			url : findall,
			reader: reader
		});
		store.load({params:{start:0, limit:10}});

		var editor = new Ext.ux.grid.RowEditor({ 
			saveText: '保存',
			cancelText:'取消',
			listeners:{
				afteredit:function(e){ 
					 var conn = new Ext.data.Connection();
		                conn.request({
		                   url: 'appDict_modify.do',
		                      params: {
		                	   'appDict.id' : e.record.get("id"),
		   	                'appDict.dictName' : e.record.get("dictName"),
		   	                  'appDict.dictCode':e.record.get("dictCode"),
		   	                  'appDict.classValue' : e.record.get("classValue"),
		   	                  'appDict.subdictCode': e.record.get("subdictCode")
		                   },
		               		success: function(resp,opt) { 
		                      e.commit(); 
		                 	},
		                     failure: function(resp,opt) { 
		                      	e.reject(); 
		                   	}
		                })
                } 
			} 
		}); 
	
		var grid = new Ext.grid.GridPanel({ 
			renderTo: 'hello',
			frame: true,
			title: '字典管理',
            autoScroll: true,
            width: "auto", 
            height: 500, 
            store: store,
            plugins: [editor],
            sm: sm,
            cm: cm,
			tbar: [{
				text: ' 增加 ', 
                handler: function(e) {addHandler(e)}  //deal add
			  
            }, {
				ref: '../removeBtn',
				text: ' 删除 ', 
				disabled: true,
				handler: function(e) {delHandler(e)} //处理删除函数 
            }], 
			viewConfig:{
			columnsText:"显示/隐藏列",
			sortAscText:"正序排列",
			sortDescText:"倒序排列"
            },
			bbar: new Ext.PagingToolbar({
				pageSize: 10,
				store: store,
		    	displayInfo : true,
				afterPageText : ' 共 {0} 页',
				beforePageText : '页 ',
				firstText : '第一页 ',
				prevText : '前一页',
				nextText : '下一页',
				lastText : '最后一页',
				refreshText : '刷新',
		        displayMsg: '显示第 {0} 条到 {1} 条记录，一共 {2} 条',
		        emptyMsg: "没有记录"
			})
      
       		 }); 
		
		grid.getSelectionModel().on('selectionchange', function(sm){
			grid.removeBtn.setDisabled(sm.getCount() < 1);
		});

		//add handle to deal with add action
		var addHandler = function(e){
			    	var conn = new Ext.data.Connection();
			    	conn.request({
			      		url: 'appDict_add.do',
			      		params: {
				    		seqName:"app_dict_seq"
			      		},
			      		success: function(resp,opt) {
			        		var newId = Ext.util.JSON.decode(
			          		resp.responseText
			        	).newId;
			        grid.getStore().insert(0, 
			          	new Dict({
			          		id : newId,
							dictName : '',
							dictCode : '',
							classValue : '',
							subdictCode : ''
			            })
			        );
			        grid.startEditing(0,0);
			      },
			      failure: function(resp,opt) {
			        Ext.Msg.alert('Error','Unable to add Dict');
			      }
		 })};
				
		//删除的处理函数
		var delHandler = function(e) {
			editor.stopEditing();
			var s = grid.getSelectionModel().getSelections();
			if(!s) return false;
			Ext.Msg.show({
				title: '删除',
				buttons: Ext.MessageBox.YESNOCANCEL,
				msg: '确认删除？',
				fn: function(btn) {
					var ids = "";
					for(var i = 0; i < s.length; i++) { //要删除的记录ID拼串
						ids = ids + s[i].get("id") + ",";
					}
					var conn = new Ext.data.Connection();
					conn.request({
						url: 'appDict_delete.do',
						params: {
							delIds: ids
						},
						success: function(resp, opt) {
							for(var i = 0; i < s.length; i++) { //把store中相应的记录删掉
								grid.getStore().remove(s[i]);
							}
							store.reload();
						},
						failure: function(resp, opt) {
							Ext.Msg.alert('错误', '删除记录失败！');	
						}
					});
				}
			  });
		}
});
</script>
</head>

<body>
	<div id="hello"> 
    </div>
</body>
</html>
